package com.ckx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ckx.constants.SystemConstants;
import com.ckx.domain.entity.Comment;
import com.ckx.domain.entity.User;
import com.ckx.domain.vo.CommentVo;
import com.ckx.domain.vo.PageVo;
import com.ckx.enums.AppHttpCodeEnum;
import com.ckx.exception.SystemException;
import com.ckx.mapper.CommentMapper;
import com.ckx.service.CommentService;
import com.ckx.service.UserService;
import com.ckx.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public void addComment(Comment comment) {
        if (!StringUtils.hasText(comment.getContent())){
            //评论内容不能为空
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        //使用 MyBatis-Plus自动填充字段
        this.save(comment);
    }

    @Override
    public PageVo commentList(String type, Long articleId, Integer pageNum, Integer pageSize) {

        //根据 文章id查询对应评论
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SystemConstants.ARTICLE_COMMENT.equals(type),Comment::getArticleId, articleId);
        //获取 主评论
        wrapper.lambda().eq(Comment::getRootId, SystemConstants.ROOT_COMMENT);
        //按类型
        wrapper.lambda().eq(Comment::getType, type);
        //分页查询
        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);
        List<Comment> comments = page.getRecords();
        //封装 vo
        List<CommentVo> commentVos = toCommentVoList(comments);
        //查询所有根评论对应的子评论集合，并且赋值给对应的属性
        commentVos.forEach(commentVo -> {
            List<CommentVo> childComments = getChildren(commentVo.getId());
            commentVo.setChildren(childComments);
        });

        return new PageVo(commentVos, page.getTotal());
    }

    /**
     * 获取子评论
     * @param id 主评论的 id
     * @return
     */
    private List<CommentVo> getChildren(Long id) {

        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Comment::getRootId, id);
        wrapper.lambda().orderByAsc(Comment::getCreateTime);
        List<Comment> comments = this.list(wrapper);
        //封装 vo
        return toCommentVoList(comments);

    }
    //封装 vo
    private List<CommentVo> toCommentVoList(List<Comment> comments) {
        List<CommentVo> commentVos = BeanCopyUtil.copyBeanList(comments, CommentVo.class);
        //遍历获取每个 vo中的 username
        commentVos.forEach(commentVo -> {
            User user = userService.getById(commentVo.getCreateBy());
            commentVo.setUsername(user.getNickName());
            commentVo.setAvatar(user.getAvatar());
            //如果不是 主评论回复
            if (commentVo.getToCommentUserId() != SystemConstants.PARENT_COMMENT) {
                String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(toCommentUserName);
            }
        });
        return commentVos;
    }
}

